<template>
  <el-card class="tree-card">
    <!-- 用了一个行列布局 -->
    <DeptTree :node-data="company" :is-root="true" @addDepartments="addDepartments" />
    <hr>
    <el-tree :data="treeData" :default-expand-all="true" :props="{label:'name'}">
      <template #default="{data}">
        <!-- 用了一个行列布局 -->
        <DeptTree :node-data="data" @delDepartments="getDepartments" @addDepartments="addDepartments" @editDepartments="editDepartments" />
      </template>
    </el-tree>
    <AddDept ref="addDept" :dialog-visible.sync="showDialog" :node-data="nodeData" :tree-data="treeData" @addDepartments="getDepartments" />
  </el-card>
</template>

<script>
import DeptTree from './components/DeptTree/index.vue'
import AddDept from './components/AddDept/index.vue'
import { getDepartments } from '@/api/departments'
import { listToTreeData } from '@/utils'
export default {
  components: { DeptTree, AddDept },
  data() {
    return {
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      treeData: [],
      showDialog: false,
      nodeData: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取部门列表
    async getDepartments() {
      const { depts } = await getDepartments()
      this.treeData = listToTreeData(depts, '')
    },
    // 添加部门
    addDepartments(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },
    // 编辑部门
    async editDepartments(nodeData) {
      await this.$refs.addDept.getDepartmentDetail(nodeData.id)
      this.showDialog = true
      this.nodeData = nodeData
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
